package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.ShOrderTj;
import com.ruoyi.system.utils.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.ShOrder;
import com.ruoyi.system.service.IShOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/order")
public class ShOrderController extends BaseController
{
    @Autowired
    private IShOrderService shOrderService;

    /**
     * 查询订单信息列表
     */
    @RequiresPermissions("system:order:list")
    @GetMapping("/list")
    public TableDataInfo list(ShOrder shOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shOrder.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shOrder.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shOrder.setAppId(user.getAppId());
            }
        }
        startPage();
        List<ShOrder> list = shOrderService.selectShOrderList(shOrder);
        return getDataTable(list);
    }

    @GetMapping("/listTjAmount")
    public AjaxResult listTj(ShOrder shOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shOrder.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shOrder.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shOrder.setAppId(user.getAppId());
            }
        }
        List<ShOrderTj> list = shOrderService.selectOrderAmountTjList(shOrder);
        return success(list);
    }

    @GetMapping("/listTj")
    public AjaxResult listTjPay(ShOrder shOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shOrder.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shOrder.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shOrder.setAppId(user.getAppId());
            }
        }
        List<ShOrderTj> list = shOrderService.selectOrderTjList(shOrder);
        return success(list);
    }

    @GetMapping("/listTjPm")
    public AjaxResult listTjPm(ShOrder shOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shOrder.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shOrder.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shOrder.setAppId(user.getAppId());
            }
        }
        List<ShOrderTj> list = shOrderService.selectOrderPmList(shOrder);
        return success(list);
    }

    @GetMapping("/listTjPayPm")
    public AjaxResult listTjPayPm(ShOrder shOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shOrder.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shOrder.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shOrder.setAppId(user.getAppId());
            }
        }
        List<ShOrderTj> list = shOrderService.selectPayPmList(shOrder);
        return success(list);
    }

    /**
     * 导出订单信息列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShOrder shOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shOrder.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shOrder.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shOrder.setAppId(user.getAppId());
            }
        }
        List<ShOrder> list = shOrderService.selectShOrderList(shOrder);
        ExcelUtil<ShOrder> util = new ExcelUtil<ShOrder>(ShOrder.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @RequiresPermissions("system:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shOrderService.selectShOrderById(id));
    }

    /**
     * 新增订单信息
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShOrder shOrder)
    {
        return toAjax(shOrderService.insertShOrder(shOrder));
    }

    /**
     * 修改订单信息
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShOrder shOrder)
    {
        return toAjax(shOrderService.updateShOrder(shOrder));
    }

    /**
     * 删除订单信息
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shOrderService.deleteShOrderByIds(ids));
    }
}
